﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using NP.BPMReportPlatform.Todo.Dto;
using NP.Enterprise.Utils.Http;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Todo
{
    public class TodoService : BPMReportPlatformAppService, ITodoService
    {
        private readonly string _esbUrl;

        private readonly INPRestHttpHelper _helper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="helper"></param>
        public TodoService(IConfiguration configuration, INPRestHttpHelper helper)
        {
            _esbUrl = configuration["Nippon:ESB:ESBCommApi"];
            _helper = helper;
        }


        public async Task<PagedResultDto<ItemsItem>> GetAllAsync(GetTodoInput input)
        {
            input.Sorting = "";
            if (input.PageIndex < 1)
            {
                input.PageIndex = 1;
            }
            var headers = new Dictionary<string, string>
            {
                {"requestId", Guid.NewGuid().ToString()},
                {"trackId", Guid.NewGuid().ToString()},
                {"sourceSystem", "BPMNEW"},
                {"serviceName", "S_XXX_WXApprove_GetToDoListAsync_S"} // 获取token
            };
            var result = await _helper.ESBPostAsync<GetTodoOutput>(_esbUrl, input, headers);

            if (result.Code != 200)
            {
                return new PagedResultDto<ItemsItem>()
                {
                    Items = Array.Empty<ItemsItem>(),
                    TotalCount = 0
                };
            }

            return new PagedResultDto<ItemsItem>()
            {
                Items = result.Result.Items,
                TotalCount = result.Result.TotalCount
            };
        }

        public async Task<BaseTodoReturnDto> TopTodo(params TopTodoDto[] dto)
        {
            var headers = new Dictionary<string, string>
            {
                {"requestId", Guid.NewGuid().ToString()},
                {"trackId", Guid.NewGuid().ToString()},
                {"sourceSystem", "BPMNEW"},
                {"serviceName", "S_XXX_WXApprove_ToDoTopingAsync_S"} // 获取token
            };
            var result = await _helper.ESBPostAsync<BaseTodoReturnDto>(_esbUrl, dto, headers);

            return result;
        }

        public async Task<BaseTodoReturnDto> EliminateTodo(params EliminateTodoDto[] dto)
        {
            var headers = new Dictionary<string, string>
            {
                {"requestId", Guid.NewGuid().ToString()},
                {"trackId", Guid.NewGuid().ToString()},
                {"sourceSystem", "BPMNEW"},
                {"serviceName", "S_XXX_WXApprove_EliminateToDoAsync_S"} // 获取token
            };
            var result = await _helper.ESBPostAsync<BaseTodoReturnDto>(_esbUrl, dto, headers);

            return result;
        }
    }
}